const defaultTime = 86400000

var isLogin = false

export function setCookie(key: string, value: string, time?: number | Date) {
    let invalid = new Date();;
    if (time) {
        switch (typeof time) {
            case 'number':
                invalid.setTime(invalid.getTime() + time)
                break;
            default:
                invalid = time
        }
    } else {
        invalid.setTime(invalid.getTime() + defaultTime)
    }
     //字符串拼接cookie
     window.document.cookie = key + "=" + value + ";path=/;expires=" + invalid.toUTCString();
};

//读取cookie
export function getCookie(param: string) {
    var c_param = '';
    if (document.cookie.length > 0) {
        var arr = document.cookie.split('; '); //这里显示的格式需要切割一下自己可输出看下
        for (var i = 0; i < arr.length; i++) {
            var arr2 = arr[i].split('='); //再次切割
            //判断查找相对应的值
            if (arr2[0] == param) {
                c_param = arr2[1];
                //保存到保存数据的地方
            }
        }
        return c_param;
    }
    return c_param;
};

export function flushCookie(param: string) {
    setCookie(param, "", -1)
};


export function CheckLoginStatus(){
    var auth = getCookie('auth')
    return auth !== ''
}

export function ClearCookie(){
    flushCookie('auth')
}